package data.platform.stream.internal.query;

import data.platform.common.service.query.RuleQueryService;
import data.platform.common.stream.aggregates.Rule;
import data.platform.stream.internal.cache.RuleCache;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.util.Optional;

@Service
@Slf4j
@RequiredArgsConstructor
public class RuleQueryServiceImpl implements RuleQueryService {

    final RuleCache ruleCache;

    @Override
    public Flux<Rule> findAll() {
        return Flux.fromIterable(ruleCache.getAllRules());
    }

    @Override
    public Mono<Rule> findById(String ruleId) {
        return Mono.just(ruleCache.findRule(ruleId))
                .filter(Optional::isPresent)
                .map(Optional::get);
    }
}
